<?php
/**
 * Base.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/6/3
 * Time: 10:55
 */
namespace app\admin\controller;

use app\common\simple_rbac\Node;
use think\Controller;
use think\Hook;

abstract class Base extends Controller
{
    /**
     * 初始化操作
     * @throws \Exception
     */
    protected function _initialize()
    {
        parent::_initialize();
        /**
         * 权限验证
         */
        $user = session('admin_auth.user');
        $expire_time = session('admin_auth.expire');
        if($expire_time < time() - 3600)
        {
            // 用户过期，请重新登录
            throw new \Exception(get_url('/admin/login/index'),302);
        }
        // 取出当前访问节点ID列表
        $current = Node::getInstance()->current();
        if(!$user['is_super_admin']){
            // 不是超级管理员则验证权限(反向认证)
            if(in_array($current[0],$user['nodes']) ||
                in_array($current[1],$user['nodes']) ||
                in_array($current[2],$user['nodes']))
            {
                // 用户过期，请重新登录
                throw new \Exception("无权限访问！",403);
            }
        }
        session('admin_auth.expire',time());
    }
}